home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
007
/
xinu.arc
/
XINU5.C
< prev
next >
Wrap
Text File
|
1986-01-03
|
8KB
|
304 lines
/* ttyoin.c - ttyoin p. 172 */
# include <conf.h>
# include <kernel.h>
# include <tty.h>
# include <io.h>
# include <slu.h>
/*-----------------------------------------------------------------------------
* ttyoin -- lower-half tty device driver for output interrupts
*-----------------------------------------------------------------------------
*/
INTPROC ttyoin(iptr)
register struct tty *iptr;
{
register struct csr *cptr;
int ct;
cptr = iptr->ioaddr;
if (iptr->ehead != iptr->etail) {
cptr->ctbuf = iptr->ebuff[iptr->etail++];
if (iptr->etail >= EBUFLEN)
iptr->etail = 0;
return;
}
if (iptr->oheld) { /* honor flow control */
cptr->ctstat = SLUDISABLE;
return;
}
if ((ct=scount(iptr->osem)) < OBUFLEN) {
cptr->ctbuf = iptr->obuff[iptr->otail++];
if (iptr->otail >= OBUFLEN)
iptr->otail = 0;
if (ct > OBMINSP)
signal(iptr->osem);
else if (++(iptr->odsend) == OBMINSP) {
iptr->odsend = 0;
signaln(iptr->osem, OBMINSP);
}
} else
cptr->ctstat = SLUDISABLE;
}
/* ttyputc.c - ttyputc p. 167 */
# include <conf.h>
# include <kernel.h>
# include <tty.h>
# include <io.h>
# include <slu.h>
/*-----------------------------------------------------------------------------
* ttyputc -- write one character to a tty device
*-----------------------------------------------------------------------------
*/
ttyputc(devptr, ch)
struct devsw *devptr;
char ch;
{
struct tty *iptr;
char ps;
iptr = &tty[devptr->dvminor];
if (ch == NEWLINE && iptr->ocrlf)
ttyputc(devptr,RETURN);
wait(iptr->osem); /* wait for space in queue */
disable(ps);
iptr->obuff[iptr->ohead++] = ch;
if (iptr->ohead >= OBUFLEN)
iptr->ohead = 0;
(iptr->ioaddr)->ctstat = SLUENABLE;
restore(ps);
return(OK);
}
/* ttyreac.c - ttyread, readcopy p. 165 */
# include <conf.h>
# include <kernel.h>
# include <tty.h>
# include <io.h>
# include <slu.h>
/*-----------------------------------------------------------------------------
* ttyread -- read one or more characters from a tty device
*-----------------------------------------------------------------------------
*/
ttyread(devptr, buff, count)
struct devsw *devptr;
int count;
char *buff;
{
char ps;
register struct tty *iptr;
int avail, nread;
if (count < 0)
return(SYSERR);
disable(ps);
avail = scount ((iptr=&tty[devptr->dvminor])->isem);
if ((count = (count==0 ? avail : count)) == 0) {
restore(ps);
return(0);
}
nread = count;
if (count <= avail)
readcopy(buff, iptr, count);
else {
if (avail > 0) {
readcopy(buff, iptr, avail);
buff += avail;
count -= avail;
}
for ( ; count>0 ; count-- )
*buff++ = ttygetc(devptr);
}
restore(ps);
return(nread);
}
/*-----------------------------------------------------------------------------
* readcopy -- high speed copy procedure used by ttyread
*-----------------------------------------------------------------------------
*/
LOCAL readcopy(buff, iptr, count)
register char *buff;
struct tty *iptr;
int count;
{
register char *qtail, *qend, *uend; /* copy loop variables */
qtail = &iptr->ibuff[iptr->itail];
qend = &iptr->ibuff[IBUFLEN];
uend = buff + count;
while ( buff < uend ) {
*buff++ = *qtail++;
if ( qtail >= qend )
qtail = iptr->ibuff;
}
iptr->itail = qtail - iptr->ibuff;
sreset(iptr->isem, scount(iptr->isem)-count);
}
/* ttywrite.c - ttywrite, writcopy p. 170 */
# include <conf.h>
# include <kernel.h>
# include <tty.h>
# include <io.h>
# include <slu.h>
/*-----------------------------------------------------------------------------
* ttywrite -- write one or more characters to a tty device
*-----------------------------------------------------------------------------
*/
ttywrite(devptr, buff, count)
struct devsw *devptr;
char *buff;
int count;
{
register struct tty *ttyp;
int avail;
char ps;
if (count < 0)
return(SYSERR);
if (count == 0)
return(OK);
disable(ps);
ttyp = &tty[devptr->dvminor];
if ((avail=scount(ttyp->osem)) >= count) {
writcopy(buff, ttyp, count);
(ttyp->ioaddr)->ctstat = SLUENABLE;
} else {
if (avail > 0) {
writcopy(buff, ttyp, avail);
buff += avail;
count -= avail;
}
for ( ; count>0 ; count-- )
ttyputc(devptr, *buff++);
}
restore(ps);
return(OK);
}
/*-----------------------------------------------------------------------------
* writcopy -- highspeed copy from user's buffer into system buffer
*-----------------------------------------------------------------------------
*/
LOCAL writcopy(buff, ttyp, count)
register char *buff;
struct tty *ttyp;
int count;
{
register char *qhead, *qend, *uend;
qhead = &ttyp->obuff[ttyp->ohead];
qend = &ttyp->obuff[OBUFLEN];
uend = buff + count;
while (buff < uend) {
*qhead++ = *buff++;
if (qhead >= qend)
qhead = ttyp->obuff;
}
ttyp->ohead = qhead - ttyp->obuff;
sreset(ttyp->osem, scount(ttyp->osem)-count);
}
/* userret.c - userret p. 76 */
# include <conf.h>
# include <kernel.h>
/*-----------------------------------------------------------------------------
* userret -- entered when a process exits by return
*-----------------------------------------------------------------------------
*/
userret()
{
kill( getpid() );
}
/* wait.c - wait p. 85 */
# include <conf.h>
# include <kernel.h>
# include <proc.h>
# include <q.h>
# include <sem.h>
/*-----------------------------------------------------------------------------
* wait -- make current process wait on a semaphore
*-----------------------------------------------------------------------------
*/
SYSCALL wait(sem)
int sem;
{
char ps;
register struct sentry *sptr;
register struct pentry *pptr;
disable(ps);
if (isbadsem(sem) || (sptr = &semaph[sem])->sstate==SFREE) {
restore(ps);
return(SYSERR);
}
if (--(sptr->semcnt) < 0) {
(pptr = &proctab[currpid])->pstate = PRWAIT;
pptr->psem = sem;
enqueue(currpid,sptr->sqtail);
resched();
}
restore(ps);
return(OK);
}
/* wakeup.c - wakeup p. 133 */
# include <conf.h>
# include <kernel.h>
# include <proc.h>
# include <q.h>
# include <sleep.h>
/*-----------------------------------------------------------------------------
* wakeup -- called by clock interrupt dispatcher to awaken processes
*-----------------------------------------------------------------------------
*/
INTPROC wakeup()
{
while (nonempty(clockq) && firstkey(clockq) <= 0)
ready(getfirst(clockq),RESCHNO);
if (slnempty = nonempty(clockq))
sltop = &q[q[clockq].qnext].qkey;
resched();
}
/* write.c - write p. 148 */
# include <conf.h>
# include <kernel.h>
# include <io.h>
/*-----------------------------------------------------------------------------
* write -- write one or more bytes to a device
*-----------------------------------------------------------------------------
*/
write(descrp, buff, count)
int descrp, count;
char *buff;
{
struct devsw *devptr;
if (isbaddev(descrp))
return(SYSERR);
devptr = &devtab[descrp];
return ((*devptr->dvwrite)(devptr,buff,count));
}
/* xdone.c - xdone p. 72 */
/*-----------------------------------------------------------------------------
* xdone -- print system completion message as last process exits
*-----------------------------------------------------------------------------
*/
xdone()
{
printf("\n\nAll user processes have completed.\n\n");
}